package com._21cn.cms.template.web.controller;

import com._21cn.cms.article.entity.Article;
import com._21cn.cms.article.service.ArticleService;
import com._21cn.cms.article.service.SpecialService;
import com._21cn.cms.article.utils.ArticleHelper;
import com._21cn.cms.common.Constants;
import com._21cn.cms.common.obj.ResponseObj;
import com._21cn.cms.common.utils.ResponseUtil;
import com._21cn.cms.site.ConfigKeys;
import com._21cn.cms.site.entity.Channel;
import com._21cn.cms.site.entity.Column;
import com._21cn.cms.site.entity.Config;
import com._21cn.cms.site.entity.Region;
import com._21cn.cms.site.service.*;
import com._21cn.cms.template.entity.Template;
import com._21cn.cms.template.entity.vo.Page;
import com._21cn.cms.template.service.TemplateService;
import com._21cn.cms.template.utils.TemplateHelper;
import com._21cn.framework.base.controller.BaseController;
import com._21cn.framework.flip.PageList;
import com._21cn.framework.http.HttpRequestInfo;
import com._21cn.framework.utils.StringUtil;
import com.alibaba.fastjson.serializer.SerializerFeature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.Validator;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Controller
@RequestMapping("/admin/template/page")
public class PageController extends BaseController{

    @Autowired
    private ChannelService channelService;

    @Autowired
    private ColumnService columnService;

    @Autowired
    private ColumnCopyService columnCopyService;

    @Autowired
    private ArticleService articleService;

    @Autowired
    private SpecialService specialService;

    @Autowired
    private RegionService regionService;

    @Autowired
    private TemplateHelper templateHelper;

    @Autowired
    private ArticleHelper articleHelper;

    /**
     * 预览页面
     * @param request
     * @param response
     * @param model
     */
    @RequestMapping("/preview")
    public void previewPage(HttpServletRequest request, HttpServletResponse response, ModelMap model) {
        ResponseObj responseObj = new ResponseObj();
        HttpRequestInfo reqInfo = new HttpRequestInfo(request);
        Integer id = reqInfo.getIntParameter("demoId", -1);
        Integer type = reqInfo.getIntParameter("templateType", 0);
        String clientCode = reqInfo.getParameter("clientCode", "a");
        String content = reqInfo.getParameter("content", "");
        String result = "";
        Template template = new Template();
        template.setId(9999);
        template.setContent(content);
        template.setTemplateType(type);
        try{
            if( type.equals(Constants.TEMPLATE_TYPE_CHANNEL_INDEX) ){
                Channel channel = channelService.getById(id);
                Page page = new Page();
                page.setTemplate(template);
                page.setChannel(channel);
                page.setStaticFilePath(channel.getPath()+"/index.shtml");
                List<String> list = templateHelper.generatePage(page, true);
                result = list.get(0);
            }
            if( type.equals(Constants.TEMPLATE_TYPE_COLUMN_LIST) ){
                Column column = columnService.getById(id);
                Column srcColumn = this.columnCopyService.getSrcColumn(id);
                Page page = new Page();
                page.setTemplate(template);
                page.setColumn(column);
                page.setSrcColumn(srcColumn);
                page.setStaticFilePath(column.getPath()+"/list{1}.shtml");
                List<String> list = templateHelper.generatePage(page, true);
                result = list.get(0);
            }
            if( type.equals(Constants.TEMPLATE_TYPE_ARTICLE_CONTENT) ){
                Article article = articleService.getById(id);
                articleHelper.regulatePager(article);
                Page page = new Page();
                page.setTemplate(template);
                page.setArticle(article);
                String staticFilePath = templateHelper.getArticleStaticFilePath(article, clientCode);
                page.setStaticFilePath(staticFilePath);
                List<String> list = templateHelper.generatePage(page, true);
                result = list.get(0);
            }
            if( type.equals(Constants.TEMPLATE_TYPE_REGION_INCLUDE) ){
                Region region = regionService.getById(id);
                if( region.getType().equals(com._21cn.cms.article.Constants.REGION_TYPE_INCLUDE) ){
                    Page page = new Page();
                    page.setTemplate(template);
                    page.setRegion(region);
                    page.setStaticFilePath(region.getPath());
                    List<String> list = templateHelper.generatePage(page, true);
                    result = list.get(0);
                }
            }
            responseObj.setErrorCode(Constants.RETURN_SUCCESS_CODE);
            responseObj.setMessage(result);
        }catch (Exception e){
            responseObj.setErrorCode(Constants.RETURN_ERROR_CODE);
            responseObj.setMessage("模板预览失败！");
        }
        String pageContent = responseObj.getMessage();
        ResponseUtil.returnMsg(response, pageContent);
        return;

    }

    /**
     * 预览页面
     * @param request
     * @param response
     * @param model
     */
    @RequestMapping("/generate")
    public void generatePage(HttpServletRequest request, HttpServletResponse response, ModelMap model) {
        ResponseObj responseObj = new ResponseObj();
        HttpRequestInfo reqInfo = new HttpRequestInfo(request);
        String ids = reqInfo.getParameter("ids", "");
        Integer type = reqInfo.getIntParameter("templateType", 0);
        String clientCode = reqInfo.getParameter("clientCode", "a");
        Set<Integer> idSet = StringUtil.str2IntSet(ids);
        String result = "";
        try{
            if( type.equals(Constants.TEMPLATE_TYPE_CHANNEL_INDEX) ){
                for( Integer id : idSet){
                    templateHelper.generateChannelPage(id);
                }
            }
            if( type.equals(Constants.TEMPLATE_TYPE_COLUMN_LIST) ){
                for( Integer id : idSet){
                    templateHelper.generateColumnPage(id);
                }
            }
            if( type.equals(Constants.TEMPLATE_TYPE_REGION_INCLUDE) ){
                for( Integer id : idSet){
                    templateHelper.generateRegionPage(id);
                }
            }
            if( type.equals(Constants.TEMPLATE_TYPE_ARTICLE_CONTENT) ){
                for( Integer id : idSet){
                    Article article = articleService.getById(id);
                    articleHelper.regulatePager(article);
                    Template template = templateHelper.getArticleTemplateId(article, ConfigKeys.KEY_CONFIG_ARTICLE_TEMPLATE_PC);    //TODO
                    Page page = new Page();
                    page.setTemplate(template);
                    page.setArticle(article);
                    String staticFilePath = templateHelper.getArticleStaticFilePath(article, clientCode);
                    page.setStaticFilePath(staticFilePath);
                    List<String> list = templateHelper.generatePage(page);
                    result = list.get(0);
                }
            }
            if ( type.equals(Constants.TEMPLATE_TYPE_SPECIAL) ) {
                for (Integer id : idSet) {
                    this.templateHelper.generateSpecialPage(id);
                }
            }
            responseObj.setErrorCode(Constants.RETURN_SUCCESS_CODE);
            responseObj.setMessage("模板生成成功！");
        }catch (Exception e){
            responseObj.setErrorCode(Constants.RETURN_ERROR_CODE);
            responseObj.setMessage("模板预览失败！");
        }
        String pageContent = responseObj.getMessage();
        ResponseUtil.returnMsg(response, pageContent);
        return;

    }

}
